﻿/**

 @Name：swui.control 控制中心模块(消息通知、快捷方式)
 @Author：Zxq
 @License：MIT
    
 */
layui.define(['jquery', 'swui'], function (exports) {
    "use strict";

    var $ = layui.jquery;

    //开始菜单构造函数
    var Control = function () {
        swui.baseObject.call(this, 'control');
        this.msgList = {};
        this.scList = {};
        this.elems = {};
    };

    Control.prototype = {
        //初始化html元素及事件
        render: function (callback) {
            var othis = this;
            othis.elem = othis.configs.elem || $('.swui-console');

            othis.elems.Header = $('<h1>最新通知</h1>').appendTo(othis.elem);
            othis.elems.RollMsg = $('<div class="swui-rollmsg"></div>').appendTo(othis.elem);
            othis.elems.Messages = $('<div class="swui-message"><div class="layui-side-scroll"></div></div>').appendTo(othis.elem).children('div');
            othis.elems.Shortcuts = $('<div class="swui-shortcut"></div>').appendTo(othis.elem);
            othis.elems.shead = $('<h2><span class="extend-switch sp">展开</span></h2>').appendTo(othis.elems.Shortcuts);

            othis.setData({urlcode:'uicontrol'}, function (data) {
                othis.data = data;
                othis.renderElems();
                //调用渲染完毕的回调函数
                if (othis.Utils.isFunction(callback))
                    callback.call(this, othis);
            });
        }
        ,renderElems: function () {
            if (this.data === null) return;
            var othis = this;
            if (!this.data.msgs || this.data.msgs.length === 0)
                this.elems.Header.html('没有新通知');
            else {
                $(this.data.msgs).each(function (index, item) {
                    othis.addMessage(item);
                });
            }
            if (!this.data.shortcuts || this.data.shortcuts.length === 0)
                this.elems.shead.html('');
            else {
                $(this.data.shortcuts).each(function (index, item) {
                    othis.addShortcut(item);
                });
            }
            if (!this.data.rolls || this.data.rolls.length === 0)
                this.elems.RollMsg.html('');
            else {
                var html = '<ul>';
                $(this.data.rolls).each(function (index, item) {
                    html += '<li url="'+item.url+'">'+item.title+'</li>';
                });
                html+='</ul>';
                this.elems.RollMsg.html(html);
            }
            
            othis.resetEvents();
        }
        ,resetEvents: function (){
            var othis = this
                ,events = this.configs.events || [];
            //消息通知点击事件
            othis.resetEvent('click', '.swui-message-item', function(){
                if (!othis.Utils.isFunction(events.click))
                    return;
                var guid = $(this).data('guid');
                events.click.call(this, othis.msgList[guid]);
                othis.sp(event);//阻止事件冒泡
            }, othis.elems.Messages);
            //快捷方式展开与折叠事件
            othis.resetEvent('click', '.extend-switch', function(){
                if ($(this).text() === '展开') {
                    $(this).text('折叠');
                    $(this).parents('.swui-shortcut').addClass('extend');
                    $(this).parents('.swui-shortcut').siblings('.swui-message').css('bottom', '330px');
                } else {
                    $(this).text('展开');
                    $(this).parents('.swui-shortcut').removeClass('extend');
                    $(this).parents('.swui-shortcut').siblings('.swui-message').css('bottom', '');
                }
            }, othis.elems.shead);
            //快捷方式点击事件
            othis.resetEvent('click', '.swui-shortcut-item', function(){
                if (!othis.Utils.isFunction(events.click))
                    return;
                var code = $(this).data('code');
                events.click.call(this, othis.scList[code]);
                othis.sp(event);//阻止事件冒泡
            }, othis.elems.Shortcuts);
            var top = 0,t;
            var setRoll = function() {
                t = setInterval(function () {
                    var ul = othis.elems.RollMsg.children('ul');
                    var li = ul.children("li:first");
                    //先设置margin-top为0
                    li.css("margin-top",top);
                    //li的高度为25，故这里以25判断第一行是否走出ul。
                    if(top<-25){
                        //如果第一行已经走出ul，将top归零，为下个li做准备
                        top=0;
                        //这句话的意思是，先将首行的top设为0，拷贝该行，放置最后一行
                        //注意，如果这里不将第一行的top归零，拷贝后他将保持top=-25的状态，效果就是叠加在最后一行上。
                        li.css("margin-top",top).clone(true).appendTo (ul);
                        //移除首行，这时第二行变为首行
                        li.remove();
                    }else{
                        //如果第一行还未走出ul，top自减
                        top-=1;
                    }
                },50);
            }
            setRoll();
            othis.elems.RollMsg.find("ul li").hover(function() { clearInterval(t); }, setRoll);
        }
        //添加消息通知
        ,addMessage: function (option, callback) {
            if (!option || !option.content) return;
            var guid = this.Utils.guid();
            var html = '<div class="swui-message-item" data-guid="'+guid+'"><h2>'+option.subtitle+'</h2><div class="content">'+option.content+'</div></div>';
            var msgItem = $(html).appendTo(this.elems.Messages);
            this.msgList[guid] = option;
            if (this.Utils.isFunction(callback)) {
                callback(msgItem);
            }
            return msgItem;
        }
        //移除消息通知
        ,removeMessage: function (option, callback) {
            var msgItem;
            if (this.Utils.isString(option))
                msgItem = this.elems.Messages.find('.swui-message-item[data-code="'+option+'"]');
            else if (!option.guid) return;
            else msgItem = this.elems.Messages.find('.swui-message-item[data-code="'+option.guid+'"]');
            if (msgItem.length > 0) {
                msgItem.remove();
            }
            if (this.Utils.isFunction(callback)) {
                callback(msgItem);
            }
        }
        //添加快捷方式
        ,addShortcut: function (option, callback) {
            if (!option || !option.code) return;
            var html = '<div class="swui-shortcut-item" data-code="'+option.code+'">'+this.getIconHtml(option.icon)+'<span>'+option.name+'</span></div>';
            var scItem = $(html).appendTo(this.elems.Shortcuts);
            this.scList[option.code] = option;
            if (this.Utils.isFunction(callback)) {
                callback(scItem);
            }
        }
        //移除快捷方式
        ,removeShortcut: function (option, callback) {
            var scItem;
            if (this.Utils.isString(option))
                scItem = this.elems.Shortcuts.find('.swui-shortcut-item[data-code="'+option+'"]');
            else if (!option.code) return;
            else scItem = this.elems.Shortcuts.find('.swui-shortcut-item[data-code="'+option.code+'"]');
            if (scItem.length > 0) {
                scItem.remove();
            }
            if (this.Utils.isFunction(callback)) {
                callback(scItem);
            }
        }
    };

    var control = new Control();

    swui.control = control;

    exports(control.MOD_NAME, {});

    delete layui.control;
});